package com.lonely.fx;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.WritableImage;
import javafx.scene.input.*;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

/**
 * @date: 2022-07-28 12:01
 * @author: ztkj-hzb
 * @description: 文件拖拽 -- 属性设置
 **/
public class DragEventSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();

        Label label = new Label("这是一段文本");

        TextField textField = new TextField();

        hBox.setSpacing(50);
        hBox.setPadding(new Insets(30));
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(label, textField);

        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        //检测是否拖拽
        label.setOnDragDetected(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //开启拖拽---携带数据
                //TransferMode.COPY,这里没有限制，根据规范指定，在后面取取数据时候要对应上
                //Dragboard继承剪切板,添加了拖拽效果
                Dragboard dragboard = label.startDragAndDrop(TransferMode.MOVE);

                //定制拖拽的样式,可以用一个截图来实现
                WritableImage writableImage = new WritableImage((int) label.getWidth(), (int) label.getHeight());
                Text text = new Text(label.getText());
                text.snapshot(null, writableImage);

                dragboard.setDragView(writableImage);

                //向剪切板中添加数据
                Map<DataFormat, Object> dataFormatObjectHashMap = new HashMap<>();
                dataFormatObjectHashMap.put(DataFormat.PLAIN_TEXT, label.getText());
                dragboard.setContent(dataFormatObjectHashMap);
            }
        });

        //在Node范围内移动监听
        textField.setOnDragOver(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {

                //这段一定要加，不然无法触发 setOnDragDropped事件
                event.acceptTransferModes(TransferMode.MOVE);
            }
        });

        //进入组件范围内触发
        textField.setOnDragEntered(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                textField.setStyle("-fx-background-color: pink");
            }
        });

        //离开组件范围触发
        textField.setOnDragExited(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                textField.setStyle("-fx-background-color: #FFFAFA");

            }
        });


        //松开手势触发
        textField.setOnDragDropped(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {

                //读取剪切板数据
                Dragboard dragboard = event.getDragboard();

                if (dragboard.hasString()) {
                    textField.setText(dragboard.getString());
                    textField.positionCaret(textField.getText().length());
                }

                //设置事件是否完成,如果设置为true
                //event.setDropCompleted(true);

            }
        });


        label.setOnDragDone(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                //如果要实现剪切效果，可以在拖拽结束后，复制为空即可
                label.setText("");
            }
        });
    }
}
